﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace UpLoad
{
    public class VideoUpLoad : UpLoadBase
    {
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="uploadfile"></param>
        /// <returns></returns>
        public override ReturnModel SaveFile(System.Web.UI.WebControls.FileUpload uploadfile)
        {
            ReturnModel model = new ReturnModel();
            string suffix = GetSuffix(uploadfile.FileName);     //获取后缀
            string filename = GetOnlyName(suffix);              //获取上传文件名
            string uploadPath = Config.VideoUploadPath.Trim() == "" ? Config.DefaultPath : Config.VideoUploadPath;      //上传路径
            string fullname = Path.Combine(uploadPath, filename);       //获取完整的上传路径和文件名
            if (!Config.videosuffixs.Contains(suffix))          //判断是否为正确的上传类型
            {
                model.IsSuccess = false;
            }
            else if (UpLoad(uploadfile, fullname))   //判断是否能上传成功
            {
                FileInfoModel info = new FileInfoModel();
                info.filename = filename;
                info.filetype = GetFileType(suffix);
                info.createtime = DateTime.Now;
                info.updatetime = DateTime.Now;
                model.FileInfo = info;
                if(new FileDal().AddFile(model.FileInfo))
                    model.IsSuccess = true;
            }
            else
            {
                model.IsSuccess = false;
            }
            return model;
        }

        /// <summary>
        /// 获取文件的完整路径
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns></returns>
        public override string GetServerPath(string filename)
        {
            string filepath = Config.VideoServerPath.Trim() == "" ? Config.DefaultServerPath : Config.VideoServerPath;
            return Path.Combine(filepath, filename).ToString();
        }

        /// <summary>
        ///删除文件
        /// </summary>
        /// <param name="filename">文件名称</param>
        /// <returns></returns>
        public override bool DeleteRecord(string filename)
        {
            string filepath = Config.VideoServerPath.Trim() == "" ? Config.DefaultServerPath : Config.VideoServerPath;
            return RemoveFile(filepath, filename);
        }

        /// <summary>
        /// 修改文件
        /// </summary>
        /// <param name="fileupload"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public override ReturnModel UpdateFile(System.Web.UI.WebControls.FileUpload fileupload, string filename)
        {
            string uploadPath = Config.VideoUploadPath.Trim() == "" ? Config.DefaultPath : Config.VideoUploadPath;      //上传路径
            string filepath = Config.VideoServerPath.Trim() == "" ? Config.DefaultServerPath : Config.VideoServerPath;
            return base.ModifyFile(fileupload, filename, uploadPath, filepath);
        }
    }
}
